package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.ParkPointApply;
import com.tbit.uqbike.client.pojo.Point;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.CalibrateService;
import com.tbit.uqbike.client.service.GeoService;
import com.tbit.uqbike.client.service.ParkPointApplyService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.DateTimeUtil;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 停车点申请
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Controller
@RequestMapping("/parkPointApply")
public class ParkPointApplyController {
    @Autowired
    private ParkPointApplyService parkPointApplyService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private GeoService geoService;


    /**
     * 申请停车点
     *
     * @param token
     * @param parkPointApply 站点申请实体类
     * @param mapType        地图类型
     * @param session        会话
     * @param response       响应
     * @ignoreParams request
     */
    @RequestMapping("/add")
    public void add(String token, ParkPointApply parkPointApply, Integer mapType, HttpSession session,
                    HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Assert.notNull(parkPointApply.getAccountId(), "-10001", "区域id不能为空");
            Assert.notNull(parkPointApply.getLo(), "-2001", "经度不能为空");
            Assert.notNull(parkPointApply.getLa(), "-2002", "纬度不能为空");
            Assert.notNull(parkPointApply.getApplyReason(), "-110001", "申请理由不能为空");

            if (mapType == null) {
                mapType = MapConstant.MAP_GOOGLE;
            }

            Integer agentId = geoService.getBelongAccount(parkPointApply.getAccountId(), parkPointApply.getLo(), parkPointApply.getLa(), mapType);
            User user = tokenService.getUser(token);
            if (agentId == null) {
                throw new BaseException("-110002", "请在运营区域内申请");
            }
            /*校准百度经纬度为原始经纬度*/
            Point point = new Point(parkPointApply.getLo(), parkPointApply.getLa());
            point = calibrateService.calibrate(point, mapType, SysConstant.ORIGINAL);
            parkPointApply.setLo(point.getX());
            parkPointApply.setLa(point.getY());

            parkPointApply.setAccountId(agentId);
            parkPointApply.setUserId(user.getUserId());
            parkPointApply.setApplyTime(DateTimeUtil.getNowTime());
            parkPointApply.setState(0);

            parkPointApplyService.insert(parkPointApply);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


}